package cn.zjgsu.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import cn.zjgsu.domain.TbTeacher;
import cn.zjgsu.domain.TbUser;
import cn.zjgsu.exception.MyException;
import cn.zjgsu.utils.SHA1;
import cn.zjgsu.domain.TbAdmin;
import cn.zjgsu.domain.TbCourseFile;
import cn.zjgsu.domain.TbStudent;
import cn.zjgsu.domain.TbTeacher;
import cn.zjgsu.service.IAdminService;
import cn.zjgsu.utils.Page;


/**
 * 管理员管理类
 * @author 颜佳钦
 *
 */

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Resource
	private IAdminService adminService;
	/**
	 * @author 颜佳钦
	 *获取教师列表并且跳转到管理员首页
	 */
	@RequestMapping("/teacherManage")
	public String getTeacherList(HttpServletRequest request){
		String pageNow = request.getParameter("pageNow");
		Page page=null;
		List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		int totalCount=adminService.getTeacherCount();

		if(totalCount!=0){
			if(pageNow!=null){
				page=new Page(totalCount,Integer.parseInt(pageNow));
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}else{
				page=new Page(totalCount, 1);
			
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("listAll", listAll);

		return "admin/teacherManage/teacherList";	
	}
	
	/**
	 * @author 颜佳钦
	 *教师单个注册
	 */
	@RequestMapping(value = "/teacherManage/singleImport", method = RequestMethod.POST)
	public String singleImport(HttpServletRequest request, TbTeacher teacher)throws Exception{
		String strMessage;
		try{
		TbUser user=new TbUser();
		user.setUsername(teacher.getTeacherName());
		user.setUsernumber(teacher.getTeacherNum());
		user.setRole("2");
	/*	new SHA1();*/
		String password=SHA1.encrypt(teacher.getTeacherPwd());
		teacher.setTeacherPwd(password);
		user.setPassword(password);
		teacher.setUser(user);
		
		adminService.addSingleTeacher(teacher);
		strMessage = "<script type='text/javascript'>alert('注册成功！')</script>";
			
		request.setAttribute("message", strMessage);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('注册失败，用户已被注册！')</script>";
			 request.setAttribute("message", strMessage);
			throw new MyException("读取异常，请重新读取！");
		}
		return "forward:/admin/teacherManage/";
	}
	
	

	/**
	 * @author 颜佳钦
	 *教师的删除
	 */
	@RequestMapping("/teacherManage/delete/{pkTeacher}")
	public String deleteTeacher(@PathVariable("pkTeacher") int pkTeacher,HttpServletRequest request)throws Exception{
		
		String strMessage;
		try{
			adminService.deleteTeacher(pkTeacher);
			 strMessage = "<script type='text/javascript'>alert('删除成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('删除失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("删除失败");
		}
		
		return "forward:/admin/teacherManage/";
	}
	
	/**
	 * @author 颜佳钦
	 *教师密码的重置
	 */
	@RequestMapping("/teacherManage/updatePwd/{pkTeacher}")
	public String updateTeacher(@PathVariable("pkTeacher") int pkTeacher,HttpServletRequest request)throws Exception{
	
		String strMessage;
		try{
			adminService.updateTeacherPwd(pkTeacher);
			 strMessage = "<script type='text/javascript'>alert('重置成功，密码为123456！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			 strMessage = "<script type='text/javascript'>alert('重置失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("重置失败");
		}
		return "forward:/admin/teacherManage/";
	}
	
	/**
	 * @author 颜佳钦
	 *获取学生列表并且跳转到列表页面
	 */
	@RequestMapping("/studentManage")
	public String getStudentList(HttpServletRequest request){
		String pageNow = request.getParameter("pageNow");
		Page page=null;
		List<TbStudent>  listAll=new ArrayList<TbStudent>();
				
		int totalCount=adminService.getStudentCount();

		if(totalCount!=0){
			if(pageNow!=null){
				page=new Page(totalCount,Integer.parseInt(pageNow));
				
	
				listAll=adminService.selectByPage2(page.getStartPos(),page.getPageSize());
			}else{
				page=new Page(totalCount, 1);
			
				listAll=adminService.selectByPage2(page.getStartPos(),page.getPageSize());
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("listAll", listAll);

		return "admin/studentManage/studentList";	
	}
	
	/**
	 * @author 颜佳钦
	 *学生单个注册
	 */
	@RequestMapping(value = "/studentManage/singleStudentImport", method = RequestMethod.POST)
	public String singleStudentImport(HttpServletRequest request, TbStudent student)throws Exception{
		String strMessage;
		try{
		TbUser user=new TbUser();
	
		user.setUsername(student.getStudentName());
		user.setUsernumber(student.getStudentNum());
		user.setRole("3");
	/*	new SHA1();*/
		String password=SHA1.encrypt(student.getStudentPwd());
		student.setStudentPwd(password);
		user.setPassword(password);
		student.setUser(user);
		
		adminService.addSingleStudent(student);
		strMessage = "<script type='text/javascript'>alert('注册成功！')</script>";
			
		request.setAttribute("message", strMessage);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('注册失败,用户已被注册！')</script>";
			 request.setAttribute("message", strMessage);
			throw new MyException("读取异常，请重新读取！");
		}
		return "forward:/admin/studentManage/";
	}
	
	

	/**
	 * @author 颜佳钦
	 *学生的删除
	 */
	@RequestMapping("/studentManage/delete/{pkStudent}")
	public String deleteStudent(@PathVariable("pkStudent") int pkStudent,HttpServletRequest request)throws Exception{
		
		String strMessage;
		try{
			adminService.deleteStudent(pkStudent);
			 strMessage = "<script type='text/javascript'>alert('删除成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('删除失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("删除失败");
		}
		
		return "forward:/admin/studentManage/";
	}
	
	/**
	 * @author 颜佳钦
	 *学生密码的重置
	 */
	@RequestMapping("/studentManage/updatePwd/{pkStudent}")
	public String updateStudent(@PathVariable("pkStudent") int pkStudent,HttpServletRequest request)throws Exception{
	
		String strMessage;
		try{
			adminService.updateStudentPwd(pkStudent);
			 strMessage = "<script type='text/javascript'>alert('重置成功，密码为123456！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			 strMessage = "<script type='text/javascript'>alert('重置失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("重置失败");
		}
		return "forward:/admin/studentManage/";
	}
	
	/**
	 * @author 颜佳钦
	 *教师批量导入
	 */
	@RequestMapping("/teacherManage/excelImport")
	public String excelImport(HttpServletRequest request,MultipartFile excelFile) throws Exception{
		String strMessage;
		if(excelFile==null){
			
			strMessage = "<script type='text/javascript'>alert('导入失败，文件不存在！')</script>";
			
			request.setAttribute("message", strMessage);
			return null;
		}
		String fileName=excelFile.getOriginalFilename();
		long size=excelFile.getSize();
		if(fileName==null || fileName.equals("") &&size==0){
	       strMessage = "<script type='text/javascript'>alert('导入失败，文件格式有问题！')</script>";
			
			request.setAttribute("message", strMessage);
			return null;
		}
	
		try {
			InputStream in=excelFile.getInputStream();
		
			adminService.impotTeacherExcel(in,fileName);
	         strMessage = "<script type='text/javascript'>alert('覆盖注册成功！')</script>";
			
			request.setAttribute("message", strMessage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('导入失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("读取异常，请重新读取！");
		}
		return "forward:/admin/teacherManage/";
	}
	/**
	 * @author 颜佳钦
	 *学生批量导入
	 */
	@RequestMapping("/studentManage/excelImport")
	public String studentExcelImport(HttpServletRequest request,MultipartFile excelFile) throws Exception{
		String strMessage;
		if(excelFile==null){
			
			strMessage = "<script type='text/javascript'>alert('导入失败，文件不存在！')</script>";
			
			request.setAttribute("message", strMessage);
			return null;
		}
		String fileName=excelFile.getOriginalFilename();
		long size=excelFile.getSize();
		if(fileName==null || fileName.equals("") &&size==0){
	       strMessage = "<script type='text/javascript'>alert('导入失败，文件格式有问题！')</script>";
			
			request.setAttribute("message", strMessage);
			return null;
		}
	
		try {
			InputStream in=excelFile.getInputStream();
		
			adminService.impotStudentExcel(in,fileName);
	         strMessage = "<script type='text/javascript'>alert('覆盖注册成功！')</script>";
			
			request.setAttribute("message", strMessage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('导入失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("读取异常，请重新读取！");
		}
		return "forward:/admin/studentManage/";
	}
	
	
	
	@RequestMapping("/teacherManage/searchTeacher")
	public String searchTeacher(HttpServletRequest request){
		/*String pageNow = request.getParameter("pageNow");
		Page page=null;
		List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		int totalCount=adminService.getTeacherCount();

		if(totalCount!=0){
			if(pageNow!=null){
				page=new Page(totalCount,Integer.parseInt(pageNow));
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}else{
				page=new Page(totalCount, 1);
			
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("listAll", listAll);*/
	/*	List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		request.setAttribute("listAll", listAll);*/
		return "admin/teacherManage/searchTeacher";	
	}
	
	
	/**
	 * @author 颜佳钦
	 *根据教工号搜索教师
	 */
	@RequestMapping("/teacherManage/searchByTeacherNum")
	public String searchByTeacherNum(TbTeacher teacher ,HttpServletRequest request)
	{
		System.out.println(teacher.getTeacherNum());
		String strMessage;
		List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		listAll=adminService.searchByTeacherNum(teacher.getTeacherNum());
		if(listAll!=null)
		{
		request.setAttribute("listAll", listAll);
		}
		else
		{
			strMessage = "<script type='text/javascript'>alert('无匹配结果！')</script>";
				
			request.setAttribute("message", strMessage);
			
		}
		return "admin/teacherManage/searchTeacher";	
		
	}
	
	/**
	 * @author 颜佳钦
	 *根据教工姓名搜索教师
	 */
	@RequestMapping("/teacherManage/searchByTeacherName")
	public String searchByTeacherName(TbTeacher teacher ,HttpServletRequest request)
	{
		System.out.println(teacher.getTeacherName());
		String strMessage;
		List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		listAll=adminService.searchByTeacherName(teacher.getTeacherName());
		if(listAll!=null)
		{
		request.setAttribute("listAll", listAll);
		}
		else
		{
			strMessage = "<script type='text/javascript'>alert('无匹配结果！')</script>";
				
			request.setAttribute("message", strMessage);
			
		}
		return "admin/teacherManage/searchTeacher";	
		
	}
	
	
	
	
	
	
	
	
	
	
	@RequestMapping("/studentManage/searchStudent")
	public String searchStudent(HttpServletRequest request){
		/*String pageNow = request.getParameter("pageNow");
		Page page=null;
		List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		int totalCount=adminService.getTeacherCount();

		if(totalCount!=0){
			if(pageNow!=null){
				page=new Page(totalCount,Integer.parseInt(pageNow));
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}else{
				page=new Page(totalCount, 1);
			
				listAll=adminService.selectByPage(page.getStartPos(),page.getPageSize());
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("listAll", listAll);*/
	/*	List<TbTeacher>  listAll=new ArrayList<TbTeacher>();
		request.setAttribute("listAll", listAll);*/
		return "admin/studentManage/searchStudent";	
	}
	
	/**
	 * @author 颜佳钦
	 *根据学号搜索学生
	 */
	@RequestMapping("/studentManage/searchByStudentNum")
	public String searchByStudentNum(TbStudent student ,HttpServletRequest request)
	{

		String strMessage;
		List<TbStudent>  listAll=new ArrayList<TbStudent>();
		listAll=adminService.searchByStudentNum(student.getStudentNum());
		if(listAll!=null)
		{
		request.setAttribute("listAll", listAll);
		}
		else
		{
			strMessage = "<script type='text/javascript'>alert('无匹配结果！')</script>";
				
			request.setAttribute("message", strMessage);
			
		}
		return "admin/studentManage/searchStudent";	
		
	}
	
	/**
	 * @author 颜佳钦
	 *根据学生姓名搜索学生
	 */
	@RequestMapping("/studentManage/searchByStudentName")
	public String searchByStudentName(TbStudent student ,HttpServletRequest request)
	{
		
		String strMessage;
		List<TbStudent>  listAll=new ArrayList<TbStudent>();
		listAll=adminService.searchByStudentName(student.getStudentName());
		if(listAll!=null)
		{
		request.setAttribute("listAll", listAll);
		}
		else
		{
			strMessage = "<script type='text/javascript'>alert('无匹配结果！')</script>";
				
			request.setAttribute("message", strMessage);
			
		}
		return "admin/studentManage/searchStudent";	
		
	}
	
	
	/**
	 * @author 颜佳钦
	 *教师的删除
	 */
	@RequestMapping("/teacherManage/delete1/{pkTeacher}")
	public String deleteTeacher1(@PathVariable("pkTeacher") int pkTeacher,HttpServletRequest request)throws Exception{
		
		String strMessage;
		try{
			adminService.deleteTeacher(pkTeacher);
			 strMessage = "<script type='text/javascript'>alert('删除成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('删除失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("删除失败");
		}
		
		return "forward:/admin/teacherManage/searchTeacher";
	}
	
	/**
	 * @author 颜佳钦
	 *教师密码的重置
	 */
	@RequestMapping("/teacherManage/updatePwd1/{pkTeacher}")
	public String updateTeacher1(@PathVariable("pkTeacher") int pkTeacher,HttpServletRequest request)throws Exception{
	
		String strMessage;
		try{
			adminService.updateTeacherPwd(pkTeacher);
			 strMessage = "<script type='text/javascript'>alert('重置成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			 strMessage = "<script type='text/javascript'>alert('重置失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("重置失败");
		}
		return "forward:/admin/teacherManage/searchTeacher";
	}


	/**
	 * @author 颜佳钦
	 *学生的删除
	 */
	@RequestMapping("/studentManage/delete1/{pkStudent}")
	public String deleteStudent1(@PathVariable("pkStudent") int pkStudent,HttpServletRequest request)throws Exception{
		
		String strMessage;
		try{
			adminService.deleteStudent(pkStudent);
			 strMessage = "<script type='text/javascript'>alert('删除成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('删除失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("删除失败");
		}
		
		return "forward:/admin/studentManage/searchStudent";
	}
	
	/**
	 * @author 颜佳钦
	 *学生密码的重置
	 */
	@RequestMapping("/studentManage/updatePwd1/{pkStudent}")
	public String updateStudent1(@PathVariable("pkStudent") int pkStudent,HttpServletRequest request)throws Exception{
	
		String strMessage;
		try{
			adminService.updateStudentPwd(pkStudent);
			 strMessage = "<script type='text/javascript'>alert('重置成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			 strMessage = "<script type='text/javascript'>alert('重置失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("重置失败");
		}
		return "forward:/admin/studentManage/searchStudent";
	}
	
	
	
	@RequestMapping("/adminManage")
	public String adminRegister(HttpServletRequest request){
		
		return"/admin/adminManage/addAdmin";

	}
	
	@RequestMapping(value="/adminManage/addAdmin",method=RequestMethod.POST)
	public String addAdmin(HttpServletRequest request,TbAdmin admin)throws Exception{
		String strMessage;
		try{
		TbUser user=new TbUser();
		user.setUsername(admin.getAdminName());
		user.setUsernumber(admin.getAdminNum());
		user.setRole("1");
		String password=SHA1.encrypt(admin.getAdminPwd());
		user.setPassword(password);
		admin.setAdminPwd(password);
		admin.setUser(user);
		
		adminService.addAdmin(admin);
		strMessage = "<script type='text/javascript'>alert('管理员添加成功！')</script>";
			
		request.setAttribute("message", strMessage);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('管理员添加失败！')</script>";
			 request.setAttribute("message", strMessage);
			throw new MyException("读取异常，请重新读取！");
		}
		return"forward:/admin/adminManage/";

	}
	
	
	
	@RequestMapping("/adminManage/uploadCourse")
	public String uploadCourse(HttpServletRequest request){
		return "/admin/adminManage/addCourse";	
	}
	
	
	
	
	
	@RequestMapping("/adminManage/importCourse")
	public String importCourse(HttpServletRequest request,MultipartFile excelFile,String year) throws Exception{
		String strMessage;
		
		 String filepath = "D://CourseUpload/";
		 File file = new File(filepath);
		//判断上传文件的保存目录是否存在
		if (!file.exists() && !file.isDirectory()) {
		System.out.println(filepath+"目录不存在，需要创建");
		//创建目录
		file.mkdir();
		}
		
		if(excelFile==null){
			
			strMessage = "<script type='text/javascript'>alert('导入失败！')</script>";
			
			request.setAttribute("message", strMessage);

		}
		String fileName = excelFile.getOriginalFilename();
		long size = excelFile.getSize();
		//获取尾追
		String suffix=fileName.substring(fileName.lastIndexOf("."), fileName.length());
		if(!suffix.equals(".xls")&&!suffix.equals(".xlsx")||fileName==null || fileName.equals("") &&size==0){
	       strMessage = "<script type='text/javascript'>alert('非有效文件，导入失败！')</script>";
			
			request.setAttribute("message", strMessage);
		
		}
	
		try {
			String changedFileName =year+"学期课表"+ suffix;
			
			String fileAddress =filepath+changedFileName;
			
			File thefile = new File(fileAddress);
			

			if(thefile.exists())
			{
				thefile.delete();
				
			    strMessage = "<script type='text/javascript'>alert('上传失败,同名文件已存在，请删除同名文件再上传！')</script>";
					
			    request.setAttribute("message", strMessage);
			}
			else
			{
				
			excelFile.transferTo(thefile);
			
			TbCourseFile courseFile = new TbCourseFile();
			
			courseFile.setCourseFileOriginalName(fileName);
			
			courseFile.setCourseFileChangeName(changedFileName);
			
			courseFile.setCourseFileTerm(year);
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			
			System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
			
			courseFile.setCourseFileTime(df.format(new Date()));
		
			courseFile.setCourseFileAddress(fileAddress);

		    adminService.importCourseFile(courseFile);
		    
	        strMessage = "<script type='text/javascript'>alert('导入成功！')</script>";
			
			request.setAttribute("message", strMessage);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('导入失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new Exception("读取异常，请重新读取！");
		}
		return "forward:/admin/adminManage/uploadCourse";
	}
	
	
	/**
	 * @author 颜佳钦
	 *获取课表列表并且跳转该页面
	 */
	@RequestMapping("/adminManage/getCourseList")
	public String getCourseList(HttpServletRequest request){
		String pageNow = request.getParameter("pageNow");
		Page page=null;
		List<TbCourseFile>  listAll=new ArrayList<TbCourseFile>();
		int totalCount=adminService.getCourseFileCount();

		if(totalCount!=0){
			if(pageNow!=null){
				page=new Page(totalCount,Integer.parseInt(pageNow));
				listAll=adminService.selectByPage3(page.getStartPos(),page.getPageSize());
			}else{
				page=new Page(totalCount, 1);
			
				listAll=adminService.selectByPage3(page.getStartPos(),page.getPageSize());
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("listAll", listAll);

		return "admin/adminManage/courseFileList";	
	}
	
	/**
	 * @author 颜佳钦
	 *课表文件的删除
	 */
	@RequestMapping("/adminManage/deleteCourseFile/{pkCourseFile}")
	public String deleteCourseFile(@PathVariable("pkCourseFile") int pkCourseFile,HttpServletRequest request)throws Exception{
		
		String strMessage;
		try{
			TbCourseFile courseFile =adminService.selectCourseFileByPrimaryKey((long)pkCourseFile);
			
			 File thefile =new File(courseFile.getCourseFileAddress());
			 if(thefile.exists())
				{
				    thefile.delete();
				}
			
			adminService.deleteCourseFile(pkCourseFile);
			 strMessage = "<script type='text/javascript'>alert('删除成功！')</script>";
				
				request.setAttribute("message", strMessage);
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			 strMessage = "<script type='text/javascript'>alert('删除失败！')</script>";
				
				request.setAttribute("message", strMessage);
			throw new MyException("删除失败");
		}
		
		return "forward:/admin/adminManage/getCourseList";
	}
	/**
	 * @author 颜佳钦
	 *课表文件的下载
	 */
	@RequestMapping("/adminManage/downloadCourseFile/{pkCourseFile}")
	public String downloadCourseFile(@PathVariable("pkCourseFile") int pkCourseFile,HttpServletRequest request, HttpServletResponse response)throws Exception{
		
		    String strMessage;
	
			TbCourseFile courseFile =adminService.selectCourseFileByPrimaryKey((long)pkCourseFile);
			
			 File thefile =new File(courseFile.getCourseFileAddress());
			 
			 if(thefile.exists())
			 {
				    String fileName = courseFile.getCourseFileChangeName().toString(); // 文件的默认保存名
				    //解决文件名中文乱码问题
				    if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {  
				    	fileName = URLEncoder.encode(fileName, "UTF-8");  
				    } else {  
				    	fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");  
				    }  
			        // 读到流中
			        InputStream inStream = new FileInputStream(courseFile.getCourseFileAddress());// 文件的存放路径
			        // 设置输出的格式
			        response.reset();
			        response.setContentType("bin");
			        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName+ "\"");
			        // 循环取出流中的数据
			        byte[] b = new byte[100];
			        int len;
			        try {
			            while ((len = inStream.read(b)) > 0)
			                response.getOutputStream().write(b, 0, len);
			            inStream.close();
			          }
			        catch(Exception e){
						// TODO Auto-generated catch block
						 strMessage = "<script type='text/javascript'>alert('下载失败！')</script>";
							
							request.setAttribute("message", strMessage);
						throw new MyException("下载失败");
					}
			 }
			 else
		        {
		        	
			  strMessage = "<script type='text/javascript'>alert('暂无文件上传，请先上传！')</script>";
				
				request.setAttribute("message", strMessage);	
		        	
		        	
		        	
		        }
		
		
		return "forward:/admin/adminManage/getCourseList";
	}
	
	
	
}
	